    <?php defined('BASEPATH') OR exit('No direct script access allowed'); ?><!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
    <meta http-equiv="Cache-Control" content="no-siteapp" />
    <link rel="Bookmark" href="/favicon.ico" >
    <link rel="Shortcut Icon" href="/favicon.ico" />
    <!--[if lt IE 9]>
    <script type="text/javascript" src="/public/js/html5shiv.js"></script>
    <script type="text/javascript" src="/public/js/respond.min.js"></script>
    <![endif]-->
    <link rel="stylesheet" type="text/css" href="/public/plugins/h-ui/css/H-ui.min.css" />
    <link rel="stylesheet" type="text/css" href="/public/plugins/h-ui.admin/css/H-ui.admin.css" />
    <link rel="stylesheet" type="text/css" href="/public/plugins/Hui-iconfont/iconfont.css" />
    <link rel="stylesheet" type="text/css" href="/public/plugins/h-ui.admin/skin/default/skin.css" id="skin" />
    <link rel="stylesheet" type="text/css" href="/public/plugins/h-ui.admin/css/style.css" />
    <link rel="stylesheet" type="text/css" href="/public/plugins/laydate/skins/default/laydate.css"/>
    <!--[if IE 6]>
    <script type="text/javascript" src="/public/js/DD_belatedPNG_0.0.8a-min.js" ></script>
    <script>DD_belatedPNG.fix('*');</script>
    <![endif]-->
    <title>快方管理后台</title>
    <meta name="keywords" content="">
    <meta name="description" content="">
    <style type="text/css">
        .date{
            width: 100px;
        }
        .table .show_count{
            text-align: right;
            padding-right: 10px;
        }
        .search{
            width: 250px;
        }
        .title{
            padding-left: 20px;
        }
        .addCategory,.modCategory{
            display: none;
        }
    </style>
</head>
<body class="big-page">
<div class="Hui-article" style="top:0;">
    <h3 class="title">分类管理<small>&emsp;<?=$chn_name?></small></h3>
    <article class="cl pd-20" style="padding-top: 0px;">

        <div class="control">
            <a href="#" class="show_add_category btn radius">添加分类</a>
            <a href="#" class="init_category btn radius">初始化分类</a>
            <a class="go_back btn-primary btn radius">返回</a>
        </div>
        <div class="mt-10">
            <table class="table table-border table-bordered table-bg table-hover table-sort table-responsive" id="category_list">
                <thead>
                    <tr class="text-c">
                        <th>分类id</th>
                        <th>分类名称</th>
                        <th>第三方id</th>
                        <th>排序</th>
                        <th>操作</th>
                    </tr>
                </thead>
                
                <tbody>

                </tbody>
            </table>
        </div>
    </article>
    <input type="hidden" name="store_id" value="<?=$_GET['store_id']?>">
    <input type="hidden" name="chn_id" value="<?=$_GET['chn_id']?>">

    <div class="addCategory">
        <form class="add_ctegory">
            <table class="table table-border table-bordered table-bg table-hover table-sort table-responsive">
                <tr>
                    <td>编号</td>
                    <td><input type="text" class="input-text" name="category_id" /></td>
                </tr>
                <tr>
                    <td>名称</td>
                    <td><input type="text" class="input-text" name="category_name" /></td>
                </tr>
                <tr>
                    <td>排序</td>
                    <td><input type="text" class="input-text" name="category_rank" /></td>
                </tr>
                <tr>
                    <td colspan="2" style="text-align: center;">
                        <a class="submit_add_category btn btn-success radius">提交</a>
                    </td>
                </tr>
            </table>
        </form>
    </div>

    <div class="modCategory">
        <form class="mod_ctegory">
            <table class="table table-border table-bordered table-bg table-hover table-sort table-responsive">
                <tr>
                    <td>编号</td>
                    <td id="category_id"></td>
                </tr>
                <tr>
                    <td>名称</td>
                    <td><input type="text" class="input-text" id="mod_category_name" name="category_name" /></td>
                </tr>
                <tr>
                    <td>排序</td>
                    <td><input type="text" class="input-text" id="mod_category_rank" name="category_rank" /></td>
                </tr>
                <tr>
                    <td colspan="2" style="text-align: center;">
                        <input type="hidden" id="mod_other_category_id" name="other_category_id">
                        <a class="submit_mod_category btn btn-success radius">提交</a>
                    </td>
                </tr>
            </table>
        </form>
    </div>

</div>

<script type="text/javascript" src="/public/js/jquery.min.js"></script>
<script type="text/javascript" src="/public/plugins/layer/layer.js"></script>
<script type="text/javascript" src="/public/plugins/h-ui/js/H-ui.min.js"></script>
<script type="text/javascript" src="/public/plugins/h-ui.admin/js/H-ui.admin.js"></script>
<script type="text/javascript" src="/public/plugins/jquery.contextmenu/jquery.contextmenu.r2.js"></script>
<script type="text/javascript" src="/public/plugins/datatables/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="/public/plugins/laydate/laydate.js"></script>
<script type="text/javascript">
    $.ajax({
        url: window.location.href,
        type: "POST",
        dataType: "json",
        success: function(res){
            var html = "";
            for(k in res.data){
                var data = res.data[k];

                var modBtn = "<a href='#' class='show_mod_category' data-category_id='" + data.fk_category_id + "' data-other_category_name='" + data.other_category_name + "' data-other_category_rank='" + data.other_category_rank + "' data-other_category_id='" + data.other_category_id + "' data-chn_id='" + data.fk_channel_id + "' data-store_id='" + data.fk_store_id + "'>编辑</a>";


                var delBtn = "<a href='#' class='rm_category'  data-other_category_id='" + data.other_category_id + "' data-chn_id='" + data.fk_channel_id + "' data-store_id='" + data.fk_store_id + "'>删除</a>";

                html += "<tr>";
                html += "<td>" + data.fk_category_id + "</td>";
                html += "<td>" + data.other_category_name + "</td>";
                html += "<td>" + data.other_category_id + "</td>";
                html += "<td>" + data.other_category_rank + "</td>";
                html += "<td>" + modBtn + "&emsp;" + delBtn + "</td>";
                html += "</tr>";
            }

            $("#category_list tbody").html(html)
        }
    })

    $(".show_add_category").bind("click", function(){
        layer.open({
          type: 1,
          title: "添加分类",
          skin: 'layui-layer-demo', //样式类名
          closeBtn: 0, //不显示关闭按钮
          area: ['360px', '235px'],
          anim: 2,
          shadeClose: true, //开启遮罩关闭
          content: $(".addCategory").html()
        });
    })

    $(document).on("click", ".submit_add_category", function(){
        var data = $(".add_ctegory:eq(1)").serialize();
        data += "&chn_id=" + $("input[name='chn_id']").val()
        data += "&store_id=" + $("input[name='store_id']").val()

        var loading = layer.msg('处理中', {
          icon: 16
          ,shade: 0.5
        });

        $.ajax({
            url: "/ChannelCategory/addCategory",
            type: "POST",
            dataType: "json",
            data: data,
            success: function(res){
                layer.close(loading)
                if(res.status == "success"){
                    window.location.reload();
                }else{
                    layer.alert(res.info);
                }
            }
        })
    })

    $(document).on("click", ".rm_category", function(){
        var data = {
            "chn_id" : $(this).data("chn_id"),
            "store_id" : $(this).data("store_id"),
            "other_category_id" : $(this).data("other_category_id"),
        };

        layer.confirm('确认删除该分类吗', {
            btn: ['确认','取消'] //按钮
        }, function(){
            var loading = layer.msg('处理中', {
              icon: 16
              ,shade: 0.5
            });
            $.ajax({
                url: "/ChannelCategory/delCategory",
                type: "POST",
                dataType: "json",
                data: data,
                success: function(res){
                    layer.close(loading);
                    if(res.status == "success"){
                        window.location.reload();
                    }else{
                        layer.alert(res.info);
                    }
                }
            })
        }, function(){
            layer.closeAll();
        });
    })

    $(document).on("click", ".show_mod_category", function(){
        var data = $(this).data();

        $(".mod_ctegory #category_id").html(data.category_id);
        $(".mod_ctegory #mod_category_name").attr("value", data.other_category_name);
        $(".mod_ctegory #mod_category_rank").attr("value", data.other_category_rank);
        $(".mod_ctegory #mod_other_category_id").attr("value", data.other_category_id);

        layer.open({
          type: 1,
          title: "编辑分类",
          skin: 'layui-layer-demo', //样式类名
          closeBtn: 0, //不显示关闭按钮
          area: ['360px', '225px'],
          anim: 2,
          shadeClose: true, //开启遮罩关闭
          content: $(".modCategory").html()
        });
    })

    $(document).on("click", ".submit_mod_category", function(){
        var data = $(".mod_ctegory:eq(1)").serialize();

        data += "&chn_id=" + $("input[name='chn_id']").val()
        data += "&store_id=" + $("input[name='store_id']").val()

        var loading = layer.msg('处理中', {
          icon: 16
          ,shade: 0.5
        });

        $.ajax({
            url: "/ChannelCategory/updateCategory",
            type: "POST",
            dataType: "json",
            data: data,
            success: function(res){
                layer.close(loading);
                if(res.status == "success"){
                    window.location.reload();
                }else{
                    layer.alert(res.info);
                }
            }
        })
    })

    $(".init_category").bind("click", function(){
        var data = {
            "chn_id" : $("input[name='chn_id']").val(),
            "store_id" : $("input[name='store_id']").val()
        };

        var loading = layer.msg('处理中', {
          icon: 16,
          shade: 0.5
        });

        $.ajax({
            url: "/ChannelCategory/initCategory",
            type: "POST",
            dataType: "json",
            data: data,
            success: function(res){
                layer.close(loading);
                window.location.reload();
            }
        })
    })
    $(".go_back").bind("click", function(){
        window.history.back();
    })
</script>
</body>
</html>
